package com.sgb.kjwl.view.ui.adapter.category;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemCategoryMemberRightBinding;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class CategoryMemberRightAdapter extends BaseRecycleAdapter<MenuEntity<MenuEntity>> {

    private int lefPosition;

    public CategoryMemberRightAdapter(Context context, int layoutId, List<MenuEntity<MenuEntity>> datas) {
        super(context, layoutId, datas);
    }

    public void setLefPosition(int lefPosition) {
        this.lefPosition = lefPosition;
    }

    @Override
    public void convert(ItemViewHolder holder, MenuEntity menuEntity) {
        holder.setBinding(BR.viewModel, menuEntity);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ImageView imageView = ((ItemCategoryMemberRightBinding) holder.getmBinding()).ivAddress;
        switch (lefPosition) {
            case 1:
                ((ItemCategoryMemberRightBinding) holder.getmBinding()).posLayout.setVisibility(View.VISIBLE);
                if (position == datas.size() - 1) {
                    ((ItemCategoryMemberRightBinding) holder.getmBinding()).tvPos.setText("");
                } else {
                    ((ItemCategoryMemberRightBinding) holder.getmBinding()).tvPos.setText(String.valueOf(position + 1));
                }
                ((ItemCategoryMemberRightBinding) holder.getmBinding()).tvMemberName.setText(datas.get(position).getName());
                imageView.setBackgroundResource(R.mipmap.icon_member_lever);
                break;
            case 2:
                ((ItemCategoryMemberRightBinding) holder.getmBinding()).posLayout.setVisibility(View.VISIBLE);
                ((ItemCategoryMemberRightBinding) holder.getmBinding()).tvPos.setVisibility(View.GONE);
                ((ItemCategoryMemberRightBinding) holder.getmBinding()).tvMemberName.setText(datas.get(position).getSimpleName());
                setImageBg(imageView, datas.get(position).getAppIcon(), R.mipmap.ic_cachegory);
                break;
            case 0:
                ((ItemCategoryMemberRightBinding) holder.getmBinding()).posLayout.setVisibility(View.VISIBLE);
                ((ItemCategoryMemberRightBinding) holder.getmBinding()).tvPos.setVisibility(View.GONE);
                ((ItemCategoryMemberRightBinding) holder.getmBinding()).tvMemberName.setText(datas.get(position).getName());
                setImageBg(imageView, datas.get(position).getAppIcon(), R.mipmap.ic_cachegory);
                break;
            default:
                break;
        }
    }

    public void setImageBg(ImageView imageView, String url, int imageId) {
        RequestOptions options = new RequestOptions()
                .dontAnimate()
                .placeholder(imageId)
                .error(imageId)
                .centerCrop();
        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .into(imageView);
    }
}
